Page({
  onLoad({ id }) {
    // 获取通行证
    this.getPassport(id)
  },
  async getPassport(id) {
    if (!id) return
    // 请求数据接口
    const { code, data: passport } = await wx.http.get('/visitor/' + id)
    // 检测接口调用的结果
    if (code !== 10000) return wx.utils.toast('获取通行证失败!')
    // 渲染通行证
    this.setData({ ...passport })
  },

  /**
   * 倒计时结束触发
   */
  onEnd() {
    this.setData({
      validTime: 0,
    })
  },

  onShareAppMessage() {
    return {
      title: '查看通行证',
      path: '/visitor_pkg/pages/passport/index?id=' + this.data.id,
      imageUrl: 'https://enjoy-plus.oss-cn-beijing.aliyuncs.com/images/share_poster.png',
    }
  },

  async saveToLocal() {
    // 1.1 根据图片的url地址去下载图片到本地的沙盒
    const res = await wx.getImageInfo({
      src: this.data.url,
    })
    console.log(res)

    // 1.2 从本地沙盒中读取文件写入相册
    await wx.saveImageToPhotosAlbum({
      filePath: res.path,
    })
    wx.utils.toast('写入相册成功~')
  },
})
